<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/15
 * 描述:
 */
declare(strict_types=1);

namespace app\model;


namespace app\model;


use think\Model;

class BaseModel extends Model
{
    public $with = [];

    public bool $isWithTrashed = false;//是否包含软删除数据

    public function save(object|array $data = [], ?string $sequence = null): bool
    {
        if (is_array($data)) {
            $data['appid'] = appid();
            if (!$data['appid']) {
                unset($data['appid']);
            }
        } else {
            $data->appid = appid();
            if (!$data->appid) {
                unset($data->appid);
            }
        }

        return parent::save($data, $sequence);
    }

    public function data_add($data, $isSaveAll = false)
    {
        if ($isSaveAll) {
            return $this->saveAll($data);
        }
        $res = $this->save($data);
        $pk = $this->getPk();
        if (!$pk) {
            return $res;
        }
        return $res ? $this->$pk : 0;
    }

    public function data_update($condition, $data, $isSaveAll = false)
    {
        if ($isSaveAll) {
            return $this->where($condition)->saveAll($data);
        }
        try {
            $res = $this->update($data, $condition);
        } catch (\Exception $e) {
            bs_log($e->getMessage(), 'error', 'err_sql_update.log');
            return false;
        }
        return $res;
    }

    //直接删除
    public function data_del($condition)
    {
        if (!$condition) return false;
        return self::where($condition)->delete();
    }

    //软删除
    public function data_del_soft($condition, $field = 'del_time', $value = 1)
    {
        if (!$condition) return false;
        return $this->update([$field => $value], $condition);
    }


    public function data_get($condition, $field = '*', $order = '')
    {
        $query = $this->with($this->with)->order($order)->where($condition);
        if ($this->isWithTrashed) {
            $query->withTrashed();
        }
        $data = $query->field($field)->find();
        return $data;
    }

    public function data_count($condition, $field = '*', $group = '')
    {
        $query = $this->with($this->with)->where($condition);
        if ($this->isWithTrashed) {
            $query->withTrashed();
        }
        if ($group) {
            $query->group($group);
        }
        return $query->count($field);
    }

    public function data_sum($condition, $field = '*')
    {
        $query = $this->with($this->with)->where($condition);
        if ($this->isWithTrashed) {
            $query->withTrashed();
        }
        return $query->sum($field);
    }

    public function get_value($condition, $field, $order = '')
    {
        return self::where($condition)->order($order)->value($field);
    }

    public function data_list($condition = [], $field = '*', $order = 'id desc', $page = 1, $pageSize = 10)
    {
        $qurey = $this
            ->where($condition)
            ->field($field);
        if ($this->isWithTrashed) {
            $qurey->withTrashed();
        }
        return $qurey
            ->with($this->with)
            ->order($order)
            ->page(intval($page), intval($pageSize))
            ->select();
    }

    public function data_list_nopage($condition = [], $field = '*', $order = '', $group = '')
    {
        $query = $this
            ->where($condition)
            ->field($field);
        if ($this->isWithTrashed) {
            $query->withTrashed();
        }
        if ($group) {
            $query->group($group);
        }
        if ($order) {
            $query->order($order);
        }
        if ($this->with) {
            $query->with($this->with);
        }
        return $query->select()->toArray();
    }
}
